﻿
Imports System.Xml.Serialization
Imports System.Text
Imports iConverter.Extensions
Imports System.Runtime.Serialization


Namespace Configuration

    ''' <summary>
    ''' 用于储存插件提供器ID的唯一列表
    ''' </summary>
    ''' <remarks>
    ''' 支持序列化到XML
    ''' 集合的内容不会存在重复数据
    ''' </remarks>
    <Serializable()>
    Public Class PluginProviderIDList
        Inherits HashSet(Of Guid)
        Implements IXmlSerializable, ICloneable

        Public Sub New()
            
        End Sub
        Public Sub New(ByVal comparer As IEqualityComparer(Of Guid))
            MyBase.New(comparer)
            
        End Sub
        Public Sub New(ByVal collection As IEnumerable(Of Guid))
            MyBase.New(collection)
            
        End Sub
        Public Sub New(ByVal collection As IEnumerable(Of Guid), ByVal comparer As IEqualityComparer(Of Guid))
            MyBase.New(collection, comparer)
            
        End Sub
        Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
            MyBase.New(info, context)
            
        End Sub




        ''' <summary>
        ''' 添加指定插件提供器对象的ID到集合中
        ''' </summary>
        ''' <param name="item">指定的插件提供器</param>
        ''' <remarks>
        ''' 如果指定对象为空， 则不添加对象。
        ''' </remarks>
        Public Overloads Function Add(item As PluginProvider) As Boolean
            If item IsNot Nothing Then
                Return Me.Add(item.ID)
            End If
            Return False 
        End Function


        Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
            Return Nothing
        End Function

        Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
            Dim xml = XElement.Load(reader)

            Dim ids = xml...<PluginProviderID>.Select(Function(x) (x.Value))

            Dim i As Guid 
            For Each id In ids 
                If Guid.TryParse(id ,i ) Then
                    Me.Add(i)                    
                End If
            Next


        End Sub

        Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
            Dim xml = <PluginProviders>
                          <%= From id In Me
                              Select <PluginProviderID><%= id %></PluginProviderID>
                          %>
                      </PluginProviders>

           

            xml.WriteTo(writer)
           
        End Sub

#Region "Clone 实现"

        Function Clone() As PluginProviderIDList
            Return New PluginProviderIDList (Me)
        End Function

        Private Function ImeClone() As Object Implements System.ICloneable.Clone
            Return Me.Clone
        End Function

#End Region

    End Class


End Namespace

